package com.xuexiang.xui_lib.component.guidview;

import ohos.agp.colors.RgbColor;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.render.BlendMode;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.render.Path;
import ohos.agp.render.PixelMapHolder;
import ohos.agp.utils.Color;
import ohos.agp.utils.RectFloat;
import ohos.app.Context;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.media.image.PixelMap;
import ohos.media.image.common.AlphaType;
import ohos.media.image.common.PixelFormat;
import ohos.media.image.common.Size;

/**
 * ImageView with focus area animation
 *
 * @author xuexiang
 * @since 2018/11/29 上午12:48
 */
class GuideImageView extends Image {
    private static final int DEFAULT_ANIM_COUNTER = 20;
    private GuideImageView guideImageView;
    private PixelMap mBitmap;
    private Paint mBackgroundPaint, mErasePaint, mCircleBorderPaint;
    private Color mBackgroundColor = Color.TRANSPARENT;
    private int mFocusBorderSize;
    private int mRoundRectRadius = 20;
    private Calculator mCalculator;
    private int mAnimCounter;
    private int mStep = 1;
    private double mAnimMoveFactor = 1;
    private boolean mAnimationEnabled = true;
    private Path mPath;
    private RectFloat rectF;
    private int mFocusAnimationMaxValue;
    private int mFocusAnimationStep;
    private EventHandler mEventHandler;

    public GuideImageView(Context context) {
        super(context);
        init();
    }

    public GuideImageView(Context context, AttrSet attrs) {
        super(context, attrs);
        init();
    }

    public GuideImageView(Context context, AttrSet attrs, String defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    /**
     * Initializations for background and paints
     */
    private void init() {
        guideImageView = this;
        mEventHandler = new EventHandler(EventRunner.getMainEventRunner());
        ShapeElement shapeElement = new ShapeElement();
        shapeElement.setRgbColor(RgbColor.fromArgbInt(Color.TRANSPARENT.getValue()));
        setBackground(shapeElement);
        mBackgroundPaint = new Paint();
        mBackgroundPaint.setAntiAlias(true);
        mBackgroundPaint.setColor(mBackgroundColor);
        mBackgroundPaint.setAlpha(0xFF);

        mErasePaint = new Paint();
        mErasePaint.setBlendMode(BlendMode.CLEAR);
        mErasePaint.setAlpha(0xFF);
        mErasePaint.setAntiAlias(true);

        mPath = new Path();
        mCircleBorderPaint = new Paint();
        mCircleBorderPaint.setAntiAlias(true);
        Color mFocusBorderColor = Color.TRANSPARENT;
        mCircleBorderPaint.setColor(mFocusBorderColor);
        mCircleBorderPaint.setStrokeWidth(mFocusBorderSize);
        mCircleBorderPaint.setStyle(Paint.Style.STROKE_STYLE);

        rectF = new RectFloat();

        onDraw();
    }

    /**
     * Setting parameters for background an animation
     *
     * @param backgroundColor background color
     * @param calculator      calculator object for calculations
     */
    public void setParameters(Color backgroundColor, Calculator calculator) {
        mBackgroundColor = backgroundColor;
        mAnimMoveFactor = 1;
        mCalculator = calculator;
        mBackgroundPaint.setColor(mBackgroundColor);
    }

    /**
     * Setting parameters for focus border
     *
     * @param focusBorderColor
     * @param focusBorderSize
     */
    public void setBorderParameters(Color focusBorderColor, int focusBorderSize) {
        mFocusBorderSize = focusBorderSize;
        mCircleBorderPaint.setColor(focusBorderColor);
        mCircleBorderPaint.setStrokeWidth(focusBorderSize);
    }

    /**
     * Setting round rectangle radius
     *
     * @param roundRectRadius
     */
    public void setRoundRectRadius(int roundRectRadius) {
        mRoundRectRadius = roundRectRadius;
    }

    /**
     * Enable/disable animation
     *
     * @param animationEnabled
     */
    public void setAnimationEnabled(final boolean animationEnabled) {
        mAnimationEnabled = animationEnabled;
        mAnimCounter = mAnimationEnabled ? DEFAULT_ANIM_COUNTER : 0;
    }

    /**
     * Draws background and moving focus area
     */
    protected void onDraw() {
        addDrawTask(new DrawTask() {
            @Override
            public void onDraw(Component component, Canvas canvas) {
                if (mBitmap == null) {
                    PixelMap.InitializationOptions initializationOptions = new PixelMap.InitializationOptions();
                    initializationOptions.size = new Size(getWidth(), getHeight());
                    initializationOptions.pixelFormat = PixelFormat.ARGB_8888;
                    initializationOptions.editable = true;
                    initializationOptions.alphaType = AlphaType.PREMUL;
                    mBitmap = PixelMap.create(initializationOptions);
                    mBitmap.writePixels(mBackgroundColor.getValue());
                }

                PixelMapHolder holder = new PixelMapHolder(mBitmap);
                canvas.drawPixelMapHolder(holder, 0, 0, mBackgroundPaint);

                if (mCalculator.hasFocus()) {
                    if (mCalculator.getFocusShape() == FocusShape.CIRCLE) {
                        drawCircle(canvas);
                    } else {
                        drawRoundedRectangle(canvas);
                    }
                    if (mAnimationEnabled) {
                        if (mAnimCounter == mFocusAnimationMaxValue) {
                            mStep = -1 * mFocusAnimationStep;
                        } else if (mAnimCounter == 0) {
                            mStep = mFocusAnimationStep;
                        }
                        mAnimCounter = mAnimCounter + mStep;
                        mEventHandler.postTask(new Runnable() {
                            @Override
                            public void run() {
                                guideImageView.invalidate();
                            }
                        });
                    }
                }
            }
        });
    }

    /**
     * Draws focus circle
     *
     * @param canvas canvas to draw
     */
    private void drawCircle(Canvas canvas) {
        canvas.drawCircle(mCalculator.getCircleCenterX(), mCalculator.getCircleCenterY(),
                mCalculator.circleRadius(mAnimCounter, mAnimMoveFactor), mErasePaint);

        if (mFocusBorderSize > 0) {
            mPath.reset();
            mPath.moveTo(mCalculator.getCircleCenterX(), mCalculator.getCircleCenterY());
            mPath.addCircle(mCalculator.getCircleCenterX(), mCalculator.getCircleCenterY(),
                    mCalculator.circleRadius(mAnimCounter, mAnimMoveFactor), Path.Direction.CLOCK_WISE);
            canvas.drawPath(mPath, mCircleBorderPaint);
        }
    }

    /**
     * Draws focus rounded rectangle
     *
     * @param canvas canvas to draw
     */
    private void drawRoundedRectangle(Canvas canvas) {
        float left = mCalculator.roundRectLeft(mAnimCounter, mAnimMoveFactor);
        float top = mCalculator.roundRectTop(mAnimCounter, mAnimMoveFactor);
        float right = mCalculator.roundRectRight(mAnimCounter, mAnimMoveFactor);
        float bottom = mCalculator.roundRectBottom(mAnimCounter, mAnimMoveFactor);
        rectF.clear();
        rectF.fuse(left, top, right, bottom);
        canvas.drawRoundRect(rectF, mRoundRectRadius, mRoundRectRadius, mErasePaint);

        if (mFocusBorderSize > 0) {
            mPath.reset();
            mPath.moveTo(mCalculator.getCircleCenterX(), mCalculator.getCircleCenterY());
            mPath.addRoundRect(rectF, mRoundRectRadius, mRoundRectRadius, Path.Direction.CLOCK_WISE);
            canvas.drawPath(mPath, mCircleBorderPaint);
        }
    }

    /**
     * setFocusAnimationParameters
     * @param maxValue maxValue
     * @param step step
     */
    public void setFocusAnimationParameters(int maxValue, int step) {
        mFocusAnimationMaxValue = maxValue;
        mFocusAnimationStep = step;
    }
}
